package com.example.task;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.stereotype.Component;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;




@Component
public class WebPageCrawlerJob implements Job {

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        try {
            // 定义要爬取的网页 URL
            String url = "https://www.taobao.com/";

            // 发起 HTTP 请求获取网页内容
            String htmlContent = fetchWebPage(url);

            // 处理获取到的网页内容，这里简单打印出来
            System.out.println("Fetched content from " + url + ":");
            System.out.println(htmlContent);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private String fetchWebPage(String urlString) throws IOException {
        URL url = new URL(urlString);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");

        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        StringBuilder response = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
        reader.close();

        return response.toString();
    }
}